﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Support;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.Support;
using Microsoft.SharePoint;
using System.Data;

namespace MS.InformationManagement.BLL.Returns
{
    public class Returns
    {

        #region fields

        static string _radicadoId;

        #endregion

        #region Constructors

        public Returns()
        {

        }

        #endregion

        #region Public Methods

        public static IEnumerable<ReturnedDocumentDTO> QueryDocuments(string radicadoId)
        {
            _radicadoId = radicadoId; 
            return getDocumentsByRadicado(); 
        }

        #endregion

        #region Private Methods

        private static IEnumerable<ReturnedDocumentDTO> getDocumentsByRadicado()
        {  

            string queryByRadicadoId = string.Format(@"<Where>
                                                          <And>
                                                            <Contains><FieldRef Name='Document_Radicado' /><Value Type='Text'>{0}</Value></Contains>
                                                            <Or>
                                                              <Eq><FieldRef Name='ContentType' /><Value Type='Text'>{1}</Value></Eq>
                                                              <Eq><FieldRef Name='ContentType' /><Value Type='Text'>{2}</Value></Eq>
                                                            </Or>
                                                          </And>
                                                        </Where>", _radicadoId,GlobalConstants.CTTN_RADICADO_EMPRESA_SALIDA, GlobalConstants.CTTN_RADICADO_PERSONA_SALIDA);
            
            List<string> viewFields = new List<string>(){ 
                                                          CustomSiteColumns.FIELD_DOCUMENT_RADICADO, 
                                                          CustomSiteColumns.FIELD_DOCSET_SENDERRECEPT, 
                                                          CustomSiteColumns.FIELD_DOCUMENT_STATUS,
                                                          CustomSiteColumns.FIELD_DOCUMENT_COPIESQTY,
                                                          CustomSiteColumns.FIELD_SP_CREATED
                                                        };

            List<string> listFields = new List<string>() { "Title" };
            DataTable results = QuerySiteHelper.GetDataFromSiteWithElevatedPrivileges(GlobalConstants.GLOBAL_USER_LIBRARY_TEMPLATE, viewFields, listFields, queryByRadicadoId);
            return convertTableToEntities(results);
        }

        private static IEnumerable<ReturnedDocumentDTO> convertTableToEntities(DataTable inputData)
        {
            var transformedData = from row in inputData.Rows.OfType<DataRow>()
                                  select new ReturnedDocumentDTO()
                                  {
                                      RadicadoId = row[CustomSiteColumns.FIELD_DOCUMENT_RADICADO] == null ? string.Empty : row[CustomSiteColumns.FIELD_DOCUMENT_RADICADO].ToString(),
                                      Copies = row[CustomSiteColumns.FIELD_DOCUMENT_COPIESQTY] == null || row[CustomSiteColumns.FIELD_DOCUMENT_COPIESQTY].ToString() == "" ? 0 : int.Parse(row[CustomSiteColumns.FIELD_DOCUMENT_COPIESQTY].ToString()),
                                      Receiver = row[CustomSiteColumns.FIELD_DOCSET_SENDERRECEPT] == null ? string.Empty : row[CustomSiteColumns.FIELD_DOCSET_SENDERRECEPT].ToString(),
                                      SentDate = DateTime.Parse(row[CustomSiteColumns.FIELD_SP_CREATED].ToString()),
                                      Status = new SPFieldLookupValue(row[CustomSiteColumns.FIELD_DOCUMENT_STATUS].ToString()).LookupValue,
                                      ListId = row[0].ToString(),
                                      ItemId = row[2].ToString()
                                  };
            return transformedData;
        }


        #endregion
    }
}
